package god530.com.simpleapp_slide.account.tabs.account;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import god530.com.simpleapp_slide.R;
import god530.com.simpleapp_slide.account.title.TitleBarUtils;
import god530.com.simpleapp_slide.account.tabs.account.model.AccountModel;
import god530.com.simpleapp_slide.account.tabs.account.presenter.AccountDetailsPresenter;
import god530.com.simpleapp_slide.account.tabs.account.view.IAccountDetailsView;
import god530.com.simpleapp_slide.account.utils.MyLog;

public class AccountDetailsActivity extends AppCompatActivity implements IAccountDetailsView
    , View.OnClickListener{

    public static final int TYPE_UPDATE = 0;
    public static final int TYPE_INSERT = 1;

    public static final void launch(Context context, AccountModel accountModel) {
        Intent intent = new Intent(context, AccountDetailsActivity.class);
        intent.putExtra("account_item_data", accountModel);
        intent.putExtra("update", TYPE_UPDATE);
        context.startActivity(intent);
    }

    public static final void launch(Context context, int type) {
        Intent intent = new Intent();
        intent.setClass(context, AccountDetailsActivity.class);
        intent.putExtra("update", TYPE_INSERT);
        context.startActivity(intent);
    }

    private AccountModel accountModel;
    private int type;
    private AccountDetailsPresenter accountDetailsPresenter;

    private EditText accountdetails_moeny;
    private EditText accountdetails_name;
    private ImageView accountdetails_image;
    private ImageView accountdetails_color;

    private View accountdetails_checkdetails;
    private View accountdetails_ok ;
    private TextView account_item_tv_name;
    private TextView account_item_tv_price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_details);
        initData();
        initView();
        accountDetailsPresenter = new AccountDetailsPresenter(this);
    }

    public void initView(){
        TitleBarUtils titleBarUtils = new TitleBarUtils(findViewById(R.id.accountdetails_titlebar));
        if(type == TYPE_UPDATE) {
            titleBarUtils.setTitleView("账户细节");
            titleBarUtils.setRightImageView(R.drawable.delete);
        } else {
            titleBarUtils.setTitleView("新建账户");
            titleBarUtils.setRightImageView(R.drawable.complete);
        }
        titleBarUtils.setLeftViewClickListener(this);
        titleBarUtils.setRightViewClickListener(this);

        accountdetails_moeny = (EditText) findViewById(R.id.accountdetails_moeny);
        accountdetails_name = (EditText) findViewById(R.id.accountdetails_name);
        accountdetails_image = (ImageView) findViewById(R.id.accountdetails_image);
        accountdetails_color = (ImageView) findViewById(R.id.accountdetails_color);
        String money = String.valueOf(accountModel.getMoney());
        String value = accountModel.getValue();
        account_item_tv_name = (TextView) findViewById(R.id.account_item_tv_name);
        account_item_tv_price = (TextView) findViewById(R.id.account_item_tv_price);
        if(money != null) {
            accountdetails_moeny.setText(money);
            account_item_tv_price.setText(money);
        }
        if(value != null) {
            accountdetails_name.setText(value);
            account_item_tv_name.setText(value);
        }

        accountdetails_checkdetails = findViewById(R.id.accountdetails_checkdetails);
        accountdetails_ok= findViewById(R.id.accountdetails_ok);
        accountdetails_checkdetails.setOnClickListener(this);
        accountdetails_ok.setOnClickListener(this);

    };

    private void initData() {
        Intent intent = getIntent();
        type = intent.getIntExtra("update", TYPE_INSERT);
        if(type == TYPE_UPDATE) {
            accountModel = intent.getParcelableExtra("account_item_data");
        } else {
            accountModel = new AccountModel();
        }
    }


    private AccountModel getData() {
//        AccountModel accountModel = new AccountModel();
        if (accountModel == null) {
            accountModel = new AccountModel();
        }
        accountModel.setValue(accountdetails_name.getText().toString().trim());
        try{
            String trim = accountdetails_moeny.getText().toString().trim();
            accountModel.setMoney(Double.parseDouble(trim));
        } catch (Exception e) {
            Toast.makeText(this, "请输入正确的金额", Toast.LENGTH_SHORT).show();
            accountModel.setMoney(0.0D);
        }
        return accountModel;
    }

    @Override
    public Context getContext() {
        return getApplicationContext();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.title_right:
                Toast.makeText(this, "存储按钮", Toast.LENGTH_SHORT).show();
                if(type == TYPE_UPDATE) {
                    // delete
                    accountDetailsPresenter.deleteAccountItem(accountModel);
                } else if(type == TYPE_INSERT){
                    accountDetailsPresenter.saveAccountItem(getData());
                }
                finish();
                break;
            case R.id.accountdetails_checkdetails:
                // TODO 跳转到明细界面
                break;
            case R.id.accountdetails_ok:
                MyLog.i("UpdateAcount:"+accountModel);
                accountDetailsPresenter.updateAccountItem(getData());
                Toast.makeText(this, "存储", Toast.LENGTH_SHORT).show();
                finish();
                break;
        }
    }
}
